package com.zwz;

import com.zwz.constant.Constant;
import com.zwz.crawler.main.TungeeLeadsQuery;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.CookieStore;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.zwz.conf.Dbconf.*;

@Slf4j
public class WebCrawler {

    // 已访问的 URL 集合，防止重复爬取
    private static final Set<String> visitedUrls = new HashSet<>();

    // 用户代理，模拟浏览器
    private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36";


    public static void main(String[] args) {
        ExecutorService executorService= Executors.newFixedThreadPool(5);
        executorService.submit(()-> {
            try {
                crawlPage(startUrl);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        executorService.shutdown();


    }

    public static void crawlPage(String url) throws IOException, InterruptedException {

        StringBuilder content = new StringBuilder();
        // 防止重复爬取
        if (visitedUrls.contains(url)) {
           log.info("已访问过: " + url);
        }

        visitedUrls.add(url);
       log.info("正在爬取: " + url);






//        try {
            // Step 1: Scrape the webpage
            // 使用 Jsoup 发送请求，设置超时和用户代理
            Document doc = Jsoup.connect(url)
                    .userAgent(USER_AGENT)
//                    .cookie("cookie","_tx_pid=67864ea75f69e05d3b544ca   7; __wpkreporterwid_=1e252b09-1579-4065-baa9-259171f3cd56; __last_enter_version=sales; Hm_lvt_d3ee3af8af62f47558a292277356e90f=1744947754,1745221273; Qs_lvt_309705=1744948492%2C1744956213%2C1744956391%2C1744956490%2C1745221273; Qs_pv_309705=3499943857613400000%2C2401862267627224600%2C2373097984748560400%2C635693979573886800%2C1483374110121918000; _uetsid=00dcdb601e8411f08e8985a5cb4ab811; _uetvid=2a1706601c0711f0be3333b35f4687c7; _co_i=6805a5576eceb9637696f09d; _tx_uid=6805a5576eceb9637696f0a2; _tx_cid=6805a5576eceb9637696f09d; DISTINCT_ID=b2ff747e-4b39-42fe-aece-249d6b8a3d3f; _tx_sid=MTc0NTIyMTkyMTE0OTo4YTRiMzU0NTQxNTlmZWJjNjA4MTc2ZTZiNTYzMDM0Ng==; acw_tc=ac11000117452271413985702e005be2eb3df0895d542663541ca8f01a26ec; Hm_lvt_f2ee75449fc055cc4dbceb4fe403bea3=1744954848,1745221661,1745225090,1745227144; HMACCOUNT=676A9C43EBB88158; SecurityCenterDuId=ImFBV2xWMjdPdVdOMmx2Q2hGMFNTWTVFPSI.GuefEA.jn19Drl3PZMGeXgPFBDOzolLZrk; remember_token=68060d90dca9415243cb818b|6ec7100031c37f0f0589a3f79dfd6685afa47c32490f371efd4fa26e226e6a641b30de072db88db8a247b8e7e99c8cc25a2854a9a47d80f11ce295a5d9974a73; tg_referrer_source=direct; Hm_lpvt_f2ee75449fc055cc4dbceb4fe403bea3=1745227563; doncusSessionId=eyJfZnJlc2giOmZhbHNlLCJ1c2VyX2lkIjoiNjgwNWE1NTc2ZWNlYjk2Mzc2OTZmMGEyIn0.Guegvw.KGDqx-79ek6XDD7CZlEvXX0FV9U; accountCenterSessionId=.eJwtT8tqwzAQ_Bedc1jL0nrls9te6oSACSQXs5JWcVLjgu30Sf-9SullmBlmYOZbTSKxX_hN-vW1j17VicdFNuq2yNxfoqoVEiBEBzGwM4XVpgyeCvJqo8LA0yRjDr3LXf8XgjcVI6NAJCl1Sg5sBmIsXHIBPQGUgGQ1mOwbTSkxhDJTK5V2zGhKHWKF1grkAodI3qIHtkFIhKroOIKlQNFXojmh9zlvolCB9yFplmVQ9Trf8pflb9ap2dtdcxhb_bBurwG2nwC7p8Pw3J3LU9Oux-v-46gfL233UrTd-Uv9_ALQvFWz.Guegvw.Zzj07a0fDpe-WlWlzvId7q9jhuI")
                    .cookie("cookie", Constant.cookies)
                    .timeout(10_000) // 10秒超时
                    .get()

                    ;



            log.info(String.valueOf(doc));

//            if (extractedContent(content, doc)) return;
//
//            // Step 2: Save to MySQL
//            saveToDatabase(url, content.toString());
//           log.info("Content successfully scraped and saved to MySQL.");
//
//            //end
//
//            // 查找“下一页”链接
            Element nextPageLink = doc.selectFirst("a:contains(下一页), a.next"); // 根据实际标签调整
            if (nextPageLink != null && nextPageLink.hasAttr("href")) {
                String nextUrl = nextPageLink.absUrl("href"); // 获取绝对 URL
                Thread.sleep(MILLIS); // 延迟 1 秒，避免请求过快
                crawlPage(nextUrl); // 递归爬取下一页
                log.error("开始循环爬取全部数据");

            } else {
               log.info("没有下一页，爬取结束");
            }
//
//
//        } catch (IOException e) {
//            System.err.println("爬取 " + url + " 失败: " + e.getMessage());
//        } catch (InterruptedException e) {
//            System.err.println("线程中断: " + e.getMessage());
//            Thread.currentThread().interrupt(); // 恢复中断状态
//        }




    }

//    private static boolean extractedContent(StringBuilder content, Document doc) {
//        // 提取页面内容（根据实际需求修改）
//        // 示例：提取所有 <h2> 标签的文本
////            Elements titles = doc.select("h2");
////            for (Element title : titles) {
////               log.info("标题: " + title.text().trim());
////            }
//
//        //begin
//
//        Elements dataItems = doc.select(".title");
//       log.info("获取到数据条数：" + dataItems.size());
//
//        for (Element item : dataItems) {
//            String title = item.select("h3").text();
//            String value = item.select("span").text();
//           log.info("标题：" + title + " | 值：" + value);
//        }
//
//
//        if (content.length() == 0) {
//           log.info("No valid content extracted.");
//            return true;
//        }
//        return false;
//    }
//
//
//    private static void saveToDatabase(String url, String content) {
//        String sql = "INSERT INTO scraped_content (url, content) VALUES (?, ?)";
//
//        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
//             PreparedStatement pstmt = conn.prepareStatement(sql)) {
//
//            // Set parameters
//            pstmt.setString(1, url);
//            pstmt.setString(2, content);
//
//            // Execute the insert
//            pstmt.executeUpdate();
//
//        } catch (SQLException e) {
//            System.err.println("Database error: " + e.getMessage());
//        }
//    }


}